﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library;
using ZFS.Library.Helper;
using ZFS.Model;
using ZFSData;

namespace ZFS.ServerLibrary
{
    public partial class BaseService : IBaseService
    {

        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns></returns>
        public byte[] GetMenuTrees()
        {
            try
            {
                var MenuList = Factory.GetMenuManager().GetMenuTrees();
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = MenuList });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("获取菜单列表", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="MenusBytes"></param>
        /// <returns></returns>
        public byte[] UpdateMenus(byte[] MenusBytes)
        {
            try
            {
                var MenuList = ZipTools.DecompressionObject(MenusBytes) as List<tb_Menu>;
                bool result = Factory.GetMenuManager().UpdateMenus(MenuList);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("批量更新", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 查询菜单列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public byte[] GetModelsByMenu(byte[] search)
        {
            try
            {
                var MenuSearch = ZipTools.DecompressionObject(search) as tb_Menu;
                var MenuList = Factory.GetMenuManager().GetModels(MenuSearch);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = MenuList });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("查询菜单列表", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 查询菜单列表-分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="search"></param>
        /// <param name="Asc"></param>
        /// <returns></returns>
        public byte[] GetPagingModelsByMenu(int pageIndex, int pageSize, byte[] search,
            bool Asc = false)
        {
            try
            {
                int totalCount;
                var MenuSearch = ZipTools.DecompressionObject(search) as tb_Menu;
                var MenuList = Factory.GetMenuManager().GetPagingModels(pageIndex, pageSize, out totalCount, MenuSearch, Asc);
                byte[] bytes = ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = MenuList, TotalCount = totalCount });
                return bytes;
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("查询菜单列表-分页", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] DeleteEntityByMenu(byte[] entity)
        {
            try
            {
                var Menu = ZipTools.DecompressionObject(entity) as tb_Menu;
                bool result = Factory.GetMenuManager().DeleteEntity(Menu);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("删除菜单", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] UpdateEntityByMenu(byte[] entity)
        {
            try
            {
                var Menu = ZipTools.DecompressionObject(entity) as tb_Menu;
                bool result = Factory.GetMenuManager().UpdateEntity(Menu);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("更新菜单", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] AddEntityByMenu(byte[] entity)
        {
            try
            {
                var Menu = ZipTools.DecompressionObject(entity) as tb_Menu;
                var NewMenu = Factory.GetMenuManager().UpdateEntity(Menu);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = NewMenu });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("新增菜单", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 检查菜单是否存在
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public byte[] ExistEntityByMenu(byte[] entity)
        {
            try
            {
                var Menu = ZipTools.DecompressionObject(entity) as tb_Menu;
                bool result = Factory.GetMenuManager().ExistEntity(Menu);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("检查菜单是否存在", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }
    }
}
